package com.coding.dto;


import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Cynicism
 * @version 1.0
 * @project my-project
 * @description
 * @date 2024/6/11 10:22:56
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RestBean<T> {
    private int code;
    private String msg;
    private T data;

    public static <T> RestBean<T> success() {
        return new RestBean<>(200, "请求成功", null);
    }

    public static <T> RestBean<T> success(String msg) {
        return new RestBean<>(200, msg, null);
    }

    public static <T> RestBean<T> success(T data) {
        return new RestBean<>(200, "请求成功", data);
    }

    public static <T> RestBean<T> success(String msg, T data) {
        return new RestBean<>(200, msg, data);
    }

    public static <T> RestBean<T> unauthorized(String msg) {
        return new RestBean<>(401, msg, null);
    }

    public static <T> RestBean<T> failure(int code, String msg) {
        return new RestBean<>(code, msg, null);
    }

    public static <T> RestBean<T> forbidden(String msg) {
        return new RestBean<>(403, msg, null);
    }


    public boolean isSuccess() {
        return code == 200;
    }

    public String asJsonString() {
        return JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls);
    }
}
